package servlet.tea;

import bean.Result;
import bean.Student;
import bean.Teacher;
import com.fasterxml.jackson.databind.ObjectMapper;
import dao.UserDaoImpl;
import service.UserService;
import service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 李夏
 * @version 1.0
 */

//教师注册
@WebServlet("/tea/sign")
public class TeaSignServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("===============教师注册页面=============");

        String teaName = request.getParameter("teaName");
        String password = request.getParameter("password");
        int teaId = Integer.parseInt(request.getParameter("teaId"));
        int teaTel = Integer.parseInt(request.getParameter("phone"));


        UserService userService = new UserServiceImpl();
        Teacher teacher=null;
        try {
            teacher = userService.teaIsSignSuccess(password, teaId, teaName, teaTel);
        }catch (Exception e) {
            Result result= new Result();
            result.fail(e.toString());
            ObjectMapper objectMapper = new ObjectMapper();
            String str = objectMapper.writeValueAsString(result);
            response.setContentType("application/json;charset=utf8");
            response.getWriter().print(str);
        }
        Result result = new Result();
        ObjectMapper objectMapper = new ObjectMapper();
        teacher = new UserDaoImpl().teaFindByPasswordAndId(password,teaId);

        result.success(teacher);
        String str = objectMapper.writeValueAsString(result);
        response.setContentType("application/json;charset=utf8");
        response.getWriter().print(str);







    }
}
